package com.ruoyi.web.controller.app;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.app.domain.vo.AppUserCodeVo;
import com.ruoyi.app.domain.bo.AppUserCodeBo;
import com.ruoyi.app.service.IAppUserCodeService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 实名认证
 *
 * @author ruoyi
 * @date 2025-08-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/userCode")
public class AppUserCodeController extends BaseController {

    private final IAppUserCodeService iAppUserCodeService;

    /**
     * 查询实名认证列表
     */
    @SaCheckPermission("system:userCode:list")
    @GetMapping("/list")
    public TableDataInfo<AppUserCodeVo> list(AppUserCodeBo bo, PageQuery pageQuery) {
        return iAppUserCodeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出实名认证列表
     */
    @SaCheckPermission("system:userCode:export")
    @Log(title = "实名认证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppUserCodeBo bo, HttpServletResponse response) {
        List<AppUserCodeVo> list = iAppUserCodeService.queryList(bo);
        ExcelUtil.exportExcel(list, "实名认证", AppUserCodeVo.class, response);
    }

    /**
     * 获取实名认证详细信息
     *
     * @param codeId 主键
     */
    @SaCheckPermission("system:userCode:query")
    @GetMapping("/{codeId}")
    public R<AppUserCodeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long codeId) {
        return R.ok(iAppUserCodeService.queryById(codeId));
    }

    /**
     * 新增实名认证
     */
    @SaCheckPermission("system:userCode:add")
    @Log(title = "实名认证", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppUserCodeBo bo) {
        return toAjax(iAppUserCodeService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改实名认证
     */
    @SaCheckPermission("system:userCode:edit")
    @Log(title = "实名认证", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppUserCodeBo bo) {
        return toAjax(iAppUserCodeService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除实名认证
     *
     * @param codeIds 主键串
     */
    @SaCheckPermission("system:userCode:remove")
    @Log(title = "实名认证", businessType = BusinessType.DELETE)
    @DeleteMapping("/{codeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] codeIds) {
        return toAjax(iAppUserCodeService.deleteWithValidByIds(Arrays.asList(codeIds), true) ? 1 : 0);
    }
}
